package com.topc.platform.util;

import com.alibaba.fastjson.JSON;
import com.topc.base.exception.Asserts;
import com.topc.base.model.UserAuthDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;

@Slf4j
public class SecurityUtil {

    public static UserAuthDto getUser() {
        try {
            //拿 到当前用户身份
            Object principalObj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (principalObj instanceof String) {
                //取出用户身份信息
                String principal = principalObj.toString();
                //将json转成对象
                UserAuthDto user = JSON.parseObject(principal, UserAuthDto.class);
                return user;
            }
        } catch (Exception e) {
            Asserts.fail("用户数据异常");
        }
        return null;
    }
}
